﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract NAppUpdate typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class NAppUpdateDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<NAppUpdateDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.NAppUpdate _currentNAppUpdate = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxNAppUpdateDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxNAppUpdateErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxNAppUpdateBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the Version property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxVersionDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Date property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDateDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Snapshot property
		/// </summary>
		protected System.Windows.Forms.DataGridViewCheckBoxColumn uxSnapshotDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Location property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxLocationDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.NAppUpdate> _NAppUpdateList;
				
		/// <summary> 
		/// The list of NAppUpdate to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.NAppUpdate> NAppUpdateList
		{
			get {return this._NAppUpdateList;}
			set
			{
				this._NAppUpdateList = value;
				this.uxNAppUpdateBindingSource.DataSource = null;
				this.uxNAppUpdateBindingSource.DataSource = value;
				this.uxNAppUpdateDataGridView.DataSource = null;
				this.uxNAppUpdateDataGridView.DataSource = this.uxNAppUpdateBindingSource;				
				//this.uxNAppUpdateBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxNAppUpdateBindingSource_ListChanged);
				this.uxNAppUpdateBindingSource.CurrentItemChanged += new System.EventHandler(OnNAppUpdateBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnNAppUpdateBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentNAppUpdate = uxNAppUpdateBindingSource.Current as Entities.NAppUpdate;
			
			if (_currentNAppUpdate != null)
			{
				_currentNAppUpdate.Validate();
			}
			//_NAppUpdate.Validate();
			OnCurrentEntityChanged();
		}

		//void uxNAppUpdateBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.NAppUpdate"/> instance.
		/// </summary>
		public Entities.NAppUpdate SelectedNAppUpdate
		{
			get {return this._currentNAppUpdate;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxNAppUpdateDataGridView.VirtualMode;}
			set
			{
				this.uxNAppUpdateDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxNAppUpdateDataGridView.AllowUserToAddRows;}
			set {this.uxNAppUpdateDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxNAppUpdateDataGridView.AllowUserToDeleteRows;}
			set {this.uxNAppUpdateDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxNAppUpdateDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxNAppUpdateDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="NAppUpdateDataGridViewBase"/> class.
		/// </summary>
		public NAppUpdateDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxNAppUpdateDataGridView = new System.Windows.Forms.DataGridView();
			this.uxNAppUpdateBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxNAppUpdateErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxVersionDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxDateDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxSnapshotDataGridViewColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			this.uxLocationDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxNAppUpdateDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNAppUpdateBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNAppUpdateErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxNAppUpdateErrorProvider
			// 
			this.uxNAppUpdateErrorProvider.ContainerControl = this;
			this.uxNAppUpdateErrorProvider.DataSource = this.uxNAppUpdateBindingSource;						
			// 
			// uxNAppUpdateDataGridView
			// 
			this.uxNAppUpdateDataGridView.AutoGenerateColumns = false;
			this.uxNAppUpdateDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxNAppUpdateDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxVersionDataGridViewColumn,
		this.uxDateDataGridViewColumn,
		this.uxSnapshotDataGridViewColumn,
		this.uxLocationDataGridViewColumn			});
			this.uxNAppUpdateDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxNAppUpdateDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxNAppUpdateDataGridView.Name = "uxNAppUpdateDataGridView";
			this.uxNAppUpdateDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxNAppUpdateDataGridView.TabIndex = 0;	
			this.uxNAppUpdateDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxNAppUpdateDataGridView.EnableHeadersVisualStyles = false;
			this.uxNAppUpdateDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnNAppUpdateDataGridViewDataError);
			this.uxNAppUpdateDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNAppUpdateDataGridViewCellValueNeeded);
			this.uxNAppUpdateDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNAppUpdateDataGridViewCellValuePushed);
			
			//
			// uxVersionDataGridViewColumn
			//
			this.uxVersionDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxVersionDataGridViewColumn.DataPropertyName = "Version";
			this.uxVersionDataGridViewColumn.HeaderText = "Version";
			this.uxVersionDataGridViewColumn.Name = "uxVersionDataGridViewColumn";
			this.uxVersionDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxVersionDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxVersionDataGridViewColumn.ReadOnly = false;		
			//
			// uxDateDataGridViewColumn
			//
			this.uxDateDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDateDataGridViewColumn.DataPropertyName = "Date";
			this.uxDateDataGridViewColumn.HeaderText = "Date";
			this.uxDateDataGridViewColumn.Name = "uxDateDataGridViewColumn";
			this.uxDateDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDateDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDateDataGridViewColumn.ReadOnly = false;		
			//
			// uxSnapshotDataGridViewColumn
			//
			this.uxSnapshotDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxSnapshotDataGridViewColumn.DataPropertyName = "Snapshot";
			this.uxSnapshotDataGridViewColumn.HeaderText = "Snapshot";
			this.uxSnapshotDataGridViewColumn.Name = "uxSnapshotDataGridViewColumn";
			this.uxSnapshotDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxSnapshotDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxSnapshotDataGridViewColumn.ReadOnly = false;		
			//
			// uxLocationDataGridViewColumn
			//
			this.uxLocationDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxLocationDataGridViewColumn.DataPropertyName = "Location";
			this.uxLocationDataGridViewColumn.HeaderText = "Location";
			this.uxLocationDataGridViewColumn.Name = "uxLocationDataGridViewColumn";
			this.uxLocationDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxLocationDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxLocationDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxNAppUpdateDataGridView);
			this.Name = "NAppUpdateDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxNAppUpdateErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNAppUpdateDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNAppUpdateBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				NAppUpdateDataGridViewEventArgs args = new NAppUpdateDataGridViewEventArgs();
				args.NAppUpdate = _currentNAppUpdate;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class NAppUpdateDataGridViewEventArgs : System.EventArgs
		{
			private Entities.NAppUpdate	_NAppUpdate;
	
			/// <summary>
			/// the  Entities.NAppUpdate instance.
			/// </summary>
			public Entities.NAppUpdate NAppUpdate
			{
				get { return _NAppUpdate; }
				set { _NAppUpdate = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxNAppUpdateDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnNAppUpdateDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxNAppUpdateDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNAppUpdateDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNAppUpdateDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxVersionDataGridViewColumn":
						e.Value = NAppUpdateList[e.RowIndex].Version;
						break;
					case "uxDateDataGridViewColumn":
						e.Value = NAppUpdateList[e.RowIndex].Date;
						break;
					case "uxSnapshotDataGridViewColumn":
						e.Value = NAppUpdateList[e.RowIndex].Snapshot;
						break;
					case "uxLocationDataGridViewColumn":
						e.Value = NAppUpdateList[e.RowIndex].Location;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxNAppUpdateDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNAppUpdateDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNAppUpdateDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxVersionDataGridViewColumn":
						NAppUpdateList[e.RowIndex].Version = (System.String)e.Value;
						break;
					case "uxDateDataGridViewColumn":
						NAppUpdateList[e.RowIndex].Date = (System.DateTime)e.Value;
						break;
					case "uxSnapshotDataGridViewColumn":
						NAppUpdateList[e.RowIndex].Snapshot = (System.Boolean)e.Value;
						break;
					case "uxLocationDataGridViewColumn":
						NAppUpdateList[e.RowIndex].Location = (System.String)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
